package com.ck.mybatis.plus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ck.mybatis.plus.entity.User;
import com.ck.mybatis.plus.entity.condition.ConditionVo;
import com.ck.mybatis.plus.entity.dto.UserDTO;
import com.ck.mybatis.plus.entity.dto.UserPageDTO;
import com.ck.mybatis.plus.entity.vo.UserPageVO;
import com.ck.mybatis.plus.entity.vo.UserVO;
import com.ck.mybatis.plus.exception.CkException;
import com.ck.mybatis.plus.mapper.UserMapper;
import com.ck.mybatis.plus.model.CkPageInfo;
import com.ck.mybatis.plus.service.UserService;
import com.ck.mybatis.plus.util.MapperUtils;
import com.ck.mybatis.plus.util.SearchMybatisPlusUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Classname UserServiceImpl
 * @Description 用户服务
 * @Date 2020/7/11 13:59
 * @Created by lpc
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>  implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Override
    public Long insert(UserDTO userDTO) {
        User user = MapperUtils.mapping(userDTO, User.class);
        boolean save = this.save(user);
        if (save) {
            return user.getId();
        }
        throw new CkException("保存失败");
    }

    @Override
    public Boolean update(UserDTO userDTO) {
        User user = MapperUtils.mapping(userDTO, User.class);
        return this.updateById(user);
    }

    @Override
    public Boolean delete(Long id) {
        return this.removeById(id);
    }

    @Override
    public UserVO get(Long id) {
        User user = this.getById(id);
        return MapperUtils.mapping(user, UserVO.class);
    }

    @Override
    public CkPageInfo<UserPageVO> page(UserPageDTO demoPageDTO) {
        IPage<User> page = this.page(new Page<>(demoPageDTO.getPageNum(), demoPageDTO.getPageSize()),
                new QueryWrapper<User>().lambda().eq(User::getId, ""));//TODO
        CkPageInfo<UserPageVO> ckPage = MapperUtils.mappingNcPage(page, UserPageVO.class);
        return ckPage;
    }

    @Override
    public CkPageInfo<UserPageVO> pageWhere(UserPageDTO userPageDTO) throws Exception {
        List<ConditionVo> list = SearchMybatisPlusUtil.sqlWhereDo( userPageDTO );
        QueryWrapper queryWrapper = SearchMybatisPlusUtil.parseWhereSqlList( list );
        Page<UserPageDTO> page = new Page<>(1, 10);
        IPage<UserPageVO> equDicIPage = userMapper.pageWhere(page, queryWrapper);
        CkPageInfo<UserPageVO> ckPage = MapperUtils.mappingNcPage(equDicIPage, UserPageVO.class);
        return ckPage;
    }

    @Override
    public CkPageInfo<UserPageVO> pageJoinWhere(UserPageDTO userPageDTO) throws Exception {
        Page<UserPageDTO> page = new Page<>(1, 10);
        List<ConditionVo> list = SearchMybatisPlusUtil.sqlWhereDo( userPageDTO );
        QueryWrapper queryWrapper = SearchMybatisPlusUtil.parseWhereSqlList( list );

        //TODO 如果是公共字段（例如id，a表和b表都有），不走实体类注解，必须在这里单独处理
        queryWrapper.eq("b.id", userPageDTO.getId());
        IPage<UserPageVO> demoPageVOIPage = userMapper.pageJoinWhere(page, queryWrapper);
        CkPageInfo<UserPageVO> ckPage = MapperUtils.mappingNcPage(demoPageVOIPage, UserPageVO.class);
        return ckPage;
    }
}
